package com.dev.dream.exochatlockscreen.slidemenu;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dev.dream.exochatlockscreen.R;
import com.dev.dream.libs.slidemenu.model.QuestionHelpItem;

public class HelpFragment extends Fragment {

	
	ListView lv_app_ads;
	private String[] title_help;
	private String[] content_help;

	ArrayList<QuestionHelpItem> questionHelpItems;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		title_help = getResources().getStringArray(R.array.title_help);
		content_help = getResources().getStringArray(R.array.content_help);

		View rootView = inflater.inflate(R.layout.fragment_help, container,
				false);

		lv_app_ads = (ListView) rootView.findViewById(R.id.lv_help);

		questionHelpItems = new ArrayList<QuestionHelpItem>();

		for (int i = 0; i < title_help.length; i++) {
			questionHelpItems.add(new QuestionHelpItem(i, title_help[i],
					content_help[i]));
		}

		QuestionHelpAdapter adapter = new QuestionHelpAdapter(getActivity(),
				questionHelpItems);
		lv_app_ads.setAdapter(adapter);

		return rootView;
	}

	public class QuestionHelpAdapter extends BaseAdapter {

		private Context context;
		private ArrayList<QuestionHelpItem> questionHelpItems;

		public QuestionHelpAdapter(Context context,
				ArrayList<QuestionHelpItem> questionHelpItems) {
			this.context = context;
			this.questionHelpItems = questionHelpItems;
		}

		@Override
		public int getCount() {
			return questionHelpItems.size();
		}

		@Override
		public Object getItem(int position) {
			return questionHelpItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;

			Log.v("ConvertView", String.valueOf(position));

			if (convertView == null) {

				LayoutInflater vi = (LayoutInflater) getActivity()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = vi.inflate(R.layout.row_view_help, null);

				holder = new ViewHolder();

				holder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title_help);

				holder.tv_content = (TextView) convertView
						.findViewById(R.id.tv_content_help);

				convertView.setTag(holder);

				holder.tv_title.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						// holder.tv_content.setVisibility(View.VISIBLE);
						TextView tv = (TextView) v;
						QuestionHelpItem info = (QuestionHelpItem) tv.getTag();
						info.setVisible(!info.isVisible());
						notifyDataSetChanged();
						lv_app_ads.setSelectionFromTop(info.getId() - 1, 0);
					}
				});

				//

			} else {
				holder = (ViewHolder) convertView.getTag();

			}

			if (position % 2 == 0) {
				convertView.setBackgroundResource(R.drawable.list_background);
			} else {
				convertView
						.setBackgroundResource(R.drawable.list_background_alternate);
			}

			QuestionHelpItem info = questionHelpItems.get(position);

			holder.tv_title.setText(info.getTitle_help());
			holder.tv_content.setText(info.getContent_help());
			if (info.isVisible()) {
				holder.tv_content.setVisibility(View.VISIBLE);
			} else {
				holder.tv_content.setVisibility(View.GONE);
			}

			holder.tv_title.setTag(info);

			// if (info.isState_buy()
			// && !info.isState_use()) {
			// holder.tv_state_use.setText("USE");
			// }

			return convertView;
		}

		private class ViewHolder {
			TextView tv_title;
			TextView tv_content;

		}

	}
}
